﻿# coding: utf-8
import wx
from PyDevIL import *

class wxImageList:
	ID_IMAGE_FILE_NEW = wx.NewId()
	ID_IMAGE_EXIT = wx.NewId()
	ID_IMAGE_SETTING = wx.NewId()
	ID_IMAGE_EDITOR_SCENE = wx.NewId()
	ID_IMAGE_HIERARCHY = wx.NewId()
	ID_IMAGE_TERRAIN_CREATE = wx.NewId()
	ID_IMAGE_TERRAIN_HEIGHT = wx.NewId()
	ID_IMAGE_TERRAIN_LAYER = wx.NewId()
	ID_IMAGE_ASSETS = wx.NewId()
	ID_IMAGE_PYTHON_SHELL = wx.NewId()
	ID_IMAGE_GS_GENERAL = wx.NewId()
	ID_IMAGE_FOLDER_OPEN = wx.NewId()
	ID_IMAGE_FOLDER_CLOSE = wx.NewId()
	ID_IMAGE_FILE_FOLDER_OPEN = wx.NewId()
	ID_IMAGE_FILE_FOLDER_CLOSE = wx.NewId()
	ID_IMAGE_FILE_NORMAL = wx.NewId()
	ID_IMAGE_FILE_SELECTED = wx.NewId()
	ID_IMAGE_ASSETS_TYPE = wx.NewId()
	ID_IMAGE_UNDO = wx.NewId()
	ID_IMAGE_REDO = wx.NewId()
	ID_IMAGE_SELECT = wx.NewId()
	ID_IMAGE_TRANSLATION = wx.NewId()
	ID_IMAGE_ROTATION = wx.NewId()
	ID_IMAGE_SCALING = wx.NewId()
	ID_IMAGE_BUILD_SINGLE = wx.NewId()
	ID_IMAGE_PROPERTY = wx.NewId()
	ID_IMAGE_PARTICLE_SYSTEM = wx.NewId()
	ID_IMAGE_TERRAIN_HEIGHT_PAINT = wx.NewId()
	ID_IMAGE_TERRAIN_LAYER_PAINT = wx.NewId()
	ID_IMAGE_LOGIC_GRID = wx.NewId()
	
	IMAGE_BIG = 32
	IMAGE_SMALL = 16

	def __init__(self):
		self._bitmapList = {}
		
		# Initialize DevIL
		ilInit()
		iluInit()
				
	def __LoadBitmap(self, id, path):
		try:
			bmpBig = wx.Image(path).Rescale(wxImageList.IMAGE_BIG, wxImageList.IMAGE_BIG).ConvertToBitmap()
			bmpSmall = wx.Image(path).Rescale(wxImageList.IMAGE_SMALL, wxImageList.IMAGE_SMALL).ConvertToBitmap()
			self._bitmapList[id] = {wxImageList.IMAGE_BIG : bmpBig, wxImageList.IMAGE_SMALL : bmpSmall}
		except:
			path = 'Resource/dummy.png'
			bmpBig = wx.Image(path).Rescale(wxImageList.IMAGE_BIG, wxImageList.IMAGE_BIG).ConvertToBitmap()
			bmpSmall = wx.Image(path).Rescale(wxImageList.IMAGE_SMALL, wxImageList.IMAGE_SMALL).ConvertToBitmap()
			self._bitmapList[id] = {wxImageList.IMAGE_BIG : bmpBig, wxImageList.IMAGE_SMALL : bmpSmall}
		
		
	def LoadAllImages(self):
		self.__LoadBitmap(wxImageList.ID_IMAGE_FILE_NEW, 'Resource/icon/32px-Crystal_Clear_action_filenew.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_SETTING, 'Resource/icon/32px-Crystal_Clear_app_package_settings.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_EXIT, 'Resource/icon/32px-Crystal_Clear_action_exit.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_EDITOR_SCENE, 'Resource/icon/32px-Crystal_Clear_app_xeyes.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_HIERARCHY, 'Resource/icon/32px-Crystal_Clear_app_browser.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_TERRAIN_CREATE, 'Resource/icon/32px-Crystal_Clear_app_3d.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_TERRAIN_HEIGHT, 'Resource/icon/32px-Crystal_Clear_mimetype_video.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_TERRAIN_LAYER, 'Resource/icon/32px-Crystal_Clear_mimetype_video.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_ASSETS, 'Resource/icon/32px-Crystal_Clear_app_kfm_home.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_PYTHON_SHELL, 'Resource/icon/32px-Crystal_Clear_app_terminal.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_GS_GENERAL, 'Resource/icon/32px-Crystal_Clear_app_kfm_home.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_FOLDER_OPEN, 'Resource/icon/32px-Crystal_Clear_action_1uparrow.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_FOLDER_CLOSE, 'Resource/icon/32px-Crystal_Clear_action_1downarrow.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_FILE_FOLDER_OPEN, 'Resource/icon/32px-Crystal_Clear_filesystem_folder_grey_open.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_FILE_FOLDER_CLOSE, 'Resource/icon/32px-Crystal_Clear_filesystem_folder_grey.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_FILE_NORMAL, 'Resource/icon/32px-Crystal_Clear_mimetype_document.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_FILE_SELECTED, 'Resource/icon/32px-Crystal_Clear_action_apply.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_ASSETS_TYPE, 'Resource/icon/32px-Crystal_Clear_app_kguitar.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_UNDO, 'Resource/led-icons/arrow_undo.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_REDO, 'Resource/led-icons/arrow_redo.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_SELECT, 'Resource/icon/32px-Crystal_Clear_action_apply.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_TRANSLATION, 'Resource/icon/32px-Crystal_Clear_action_apply.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_ROTATION, 'Resource/icon/32px-Crystal_Clear_action_apply.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_SCALING, 'Resource/icon/32px-Crystal_Clear_action_apply.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_BUILD_SINGLE, 'Resource/icon/32px-Crystal_Clear_action_apply.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_PROPERTY, 'Resource/icon/32px-Crystal_Clear_action_apply.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_PARTICLE_SYSTEM, 'Resource/icon/32px-Crystal_Clear_action_apply.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_TERRAIN_HEIGHT_PAINT, 'Resource/icon/32px-Crystal_Clear_action_apply.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_TERRAIN_LAYER_PAINT, 'Resource/icon/32px-Crystal_Clear_action_apply.png')
		self.__LoadBitmap(wxImageList.ID_IMAGE_LOGIC_GRID, 'Resource/icon/32px-Crystal_Clear_action_apply.png')

	def GetBitmap(self, id, size):
		bmp = self._bitmapList.get(id, None)
		if bmp:
				return self._bitmapList[id][size]
		return None
	
	def LoadBitmap(self, path, size = None):
		""" use DevIL to load a bitmap """
		
		image = ilGenImage()
		ilBindImage(image)
		ilLoadImage(path)
		depth = ilGetInteger(IL_IMAGE_DEPTH)
		bpp = ilGetInteger(IL_IMAGE_BPP)


		if size != None:
			width, height = size
			iluScale(size[0], size[1], depth)
		else:
			width = ilGetInteger(IL_IMAGE_WIDTH)
			height = ilGetInteger(IL_IMAGE_HEIGHT)
		bmp = wx.EmptyBitmap(width, height, 32)
		pixelData = wx.AlphaPixelData(bmp)
		data = ilGetData()
		i = 0
		if bpp == 3:
			for pixel in pixelData:
				pixel.Set(data[i], data[i+1], data[i+2], 255)
				i+=3
		elif bpp == 4:
			for pixel in pixelData:
				pixel.Set(data[i], data[i+1], data[i+2], data[i+3])
				i+=4
		else:
			bmp = None      
		ilDeleteImage(image)
		return bmp

__builtins__['IL'] = wxImageList()